/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
/**
 * 
 */
package com.genia.toolbox.persistence.memory.criteria.field;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.criteria.field.Field;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.exception.FieldDefinitionException;

/**
 * TRhis field represent an arithmetic operation between two fields.
 */
public class MemoryMiscOperationField
    extends AbstractMemoryField
{

  /**
   * this enum contains all the operations available.
   */
  public enum MiscOperation
  {
    /**
     * the size of a collection.
     */
    SIZE
    {
      /**
       * return the result of the operation over the values.
       * 
       * @param values
       *          the operands
       * @return the result of the operation
       */
      @Override
      protected Object operate(final Object[] values)
      {
        assert (values.length == 1);
        Object value = values[0];
        if (value instanceof Collection) {
          return ((Collection<?>) value).size();
        }
        if (value instanceof Map) {
          return ((Map<?, ?>) value).size();
        }
        if (value instanceof Object[]) {
          return ((Object[]) value).length;
        }
        return 1;
      }
    },
    /**
     * the elements of a collection.
     */
    ELEMENTS
    {
      /**
       * return the result of the operation over the values.
       * 
       * @param values
       *          the operands
       * @return the result of the operation
       */
      @Override
      protected Object operate(final Object[] values)
      {
        assert (values.length == 1);
        Object value = values[0];
        if (value instanceof Collection) {
          return value;
        }
        if (value instanceof Map) {
          return ((Map<?, ?>) value).values();
        }
        if (value instanceof Object[]) {
          return Arrays.asList(((Object[]) value));
        }
        return Arrays.asList(value);
      }
    };

    /**
     * return the result of the operation over the values.
     * 
     * @param values
     *          the operands
     * @return the result of the operation
     */
    protected abstract Object operate(final Object[] values);
  }

  /**
   * the operation of this field.
   */
  private final transient MiscOperation operation;

  /**
   * the operands.
   */
  private final transient AbstractMemoryField[] fields;



  /**
   * the default constructor of this field.
   * 
   * @param operation
   *          the operation of this field
   * @param fields
   *          the operands
   */
  public MemoryMiscOperationField(final MiscOperation operation, final AbstractMemoryField... fields)
  {
    this.operation = operation;
    this.fields = fields;
  }



  /**
   * Return the value of this field for the given line of result.
   * 
   * @param lines
   *          the line of result we compute the field on
   * @return the value of this field for the given line of result
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  @Override
  public Object getValue(final List<BasicCriteriaResult<?>> lines)
      throws FieldDefinitionException, AggregateException
  {
    Object[] values = new Object[fields.length];
    for (int i = 0; i < fields.length; i++) {
      values[i] = fields[i].getValue(lines);
    }
    return operation.operate(values);
  }



  /**
   * returns if this <code>Field</code> is an aggregate.
   * 
   * @return <code>true</code> if this <code>Field</code> is an aggregate,
   *         <code>false</code> otherwise.
   */
  public boolean isAggregate()
  {
    for (Field field : fields) {
      if (field.isAggregate()) {
        return true;
      }
    }
    return false;
  }

}
